﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.DataAccess;


namespace SD.Business
{
    public static class UserBus
    {
        /// <summary>
        /// function get user form DB by account
        /// </summary>
        /// <param name="account">account that want to get infomation</param>
        /// <returns></returns>
        public static User GetUser(string account)
        {
            User user = new User();
            user = UserDao.GetUser(account);
            return user;
        }

        /// <summary>
        /// check user name and password when user login
        /// </summary>
        /// <param name="acount">account of user</param>
        /// <param name="password">password of user</param>
        /// <returns></returns>
        public static int CheckLogOn(string account, string password)
        {
            User user = new User();
            user = UserDao.GetUser(account);
            if (user != null && user.Password == password)
            {
                if (user.IsActive == true)
                    return 1;
                else
                    return 0;
            }
            else
                return -1;
        }

        /// <summary>
        /// check account and email when user forget password
        /// </summary>
        /// <param name="account">account of user</param>
        /// <param name="email">email of user</param>
        /// <returns></returns>
        public static bool CheckForgetPassword(string account, string email)
        {
            User user = new User();
            user = UserDao.GetUser(account);
            if (user != null)
            {
                if (user.Email == email)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;

            }
        }

        /// <summary>
        /// update password of user, return true when success, false when failure
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool CheckUpdatePassword(string account, string password)
        {
            bool result = UserDao.UpdatePassword(account, password);
            return result;
        }

        /// <summary>
        /// check information when user need to change password
        /// </summary>
        /// <param name="account">account of user</param>
        /// <param name="oldPass">old password of user</param>
        /// <param name="newPassword">new password that user want to change</param>
        /// <returns></returns>
        public static bool CheckUpdatePassword(string account, string oldPass, string newPassword)
        {
            bool result = false;
            //get user by account and check user is not null
            User user = GetUser(account);
            if (user != null)
            {
                //compare old password to user's password exist in DB
                if (user.Password == oldPass) // case =
                    result = UserDao.UpdatePassword(account, newPassword);  // call function update password
            }
            return result;
        }
    }
}
